<script lang="ts" setup>
import { ref, onMounted, nextTick } from "vue";
import { TreeMenuItem } from "../common/types";
const treeMenu = ref();

interface TreeMenuWrapper {
    id: string,
    item: TreeMenuItem,
    isVisible?: boolean
    isEnable?: boolean
}

const props = defineProps<{ items: TreeMenuItem[] }>();
const menus = ref<TreeMenuWrapper[]>();
let id = 0;
const show = (x: number, y: number) => {
    if (menus.value == null) {
        menus.value = [];
        for (let i = 0; i < props.items.length; i++) {
            let isVisible = true;
            if (props.items[i].isVisible != undefined) {
                isVisible = props.items[i].isVisible!();
            }
            let isEnable = true;
            if (props.items[i].isEnable != undefined) {
                isEnable = props.items[i].isEnable!();
            }
            menus.value.push({
                id: (++id).toString(),
                item: props.items[i],
                isVisible: isVisible,
                isEnable: isEnable,
            });
        }
    }
    else {
        for (let i = 0; i < menus.value.length; i++) {
            if (menus.value[i].item.isVisible != undefined) {
                menus.value[i].isVisible = menus.value[i].item.isVisible!();
            }
            if (menus.value[i].isVisible && menus.value[i].item.isEnable != undefined) {
                menus.value[i].isEnable = menus.value[i].item.isEnable!();
            }
        }
    }
    treeMenu.value.style.display = "";
    treeMenu.value.style.left = x + "px";
    nextTick(() => {
        if (y + treeMenu.value.offsetHeight > document.documentElement.clientHeight) {
            y = document.documentElement.clientHeight - treeMenu.value.offsetHeight - 2;
            if (y < 0) {
                y = 0;
            }
        }
        treeMenu.value.style.top = y + "px";
    });
    document.body.addEventListener("mousedown", hide);
}

const hide = () => {
    treeMenu.value.style.display = "none";
    document.body.removeEventListener("mousedown", hide);
}

const handleClick = (evt: MouseEvent, wrapper: TreeMenuWrapper) => {
    if (wrapper.isEnable === false) {
        evt.stopPropagation();
        return;
    }
    if (!wrapper.item.clickHandler) {
        return;
    }
    if (!(wrapper.item.clickHandler() === false)) {
        hide();
    }
    else {
        evt.stopPropagation();
    }
}

const contextMenuDefault = (evt: MouseEvent) => {
    evt.preventDefault();
}

const stopEvent = (evt: MouseEvent) => {
    evt.stopPropagation();
}

onMounted(() => {
    document.body.appendChild(treeMenu.value);
});

defineExpose({ show, hide });
</script>
<template>
    <ul ref="treeMenu" class="app-tree-menu" style="display:none" @contextmenu="contextMenuDefault($event)"
        @mousedown="stopEvent($event)">
        <template v-for="item in menus" :key="item.id">
            <li v-if="item.item.label == '-'" class="line">
                <div></div>
            </li>
            <li v-else :class="item.isEnable ? 'enabled' : 'disabled'" v-show="item.isVisible != false"
                @click="handleClick($event, item)">
                <span style="float: left;">{{ item.item.label }}</span>
                <span style="float: right;margin-left: 15px;"> {{ item.item.keyboard }}</span>
            </li>
        </template>
    </ul>
</template>

<style scoped>
.app-tree-menu {
    position: absolute;
    top: 0px;
    left: 0px;
    min-width: 120px;
    padding: 3px 0px 3px 0px;
    background-color: #FFFFFF;
    box-shadow: 0px 2px 6px rgba(63, 71, 102, 0.2);
    font-size: 12px;
    z-index: 6000;
    border-radius: 3px;
}

.app-tree-menu li {
    list-style: none;
    height: 24px;
    line-height: 24px;
    cursor: pointer;
    padding: 0px 14px 0px 14px;
    user-select: none;
    color: #111111;
    border: 1px solid transparent;
}

.app-tree-menu li.disabled {
    color: #AAAAAA;
    cursor: not-allowed;
}

.app-tree-menu li.line {
    height: 7px;
    line-height: 7px;
    border: none;
    display: flex;
    align-items: center;
}

.app-tree-menu li.line div {
    width: 100%;
    height: 1px;
    background-color: #CCCCCC;
}

.app-tree-menu li.enabled:hover {
    background-color: #F2F2F2;
    /* border-color: var(--color-deep-1); */
}
</style>